﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

///<summary>
///
///</summary>
public class ShopView : MonoBehaviour
{
    //关闭委托
    public UnityAction Close;
    //购买委托
    public UnityAction Buy;
    //右移委托
    public UnityAction Right;
    //左移委托
    public UnityAction Left;

    //关闭按钮
    GameObject btnClose;
    //购买按钮
    GameObject btnBuy;
    //左移按钮
    GameObject btnLeft;
    //右移按钮
    GameObject btnRight;
    //面板父物体
    GameObject obj;
    //shop面板物体
    GameObject shopParent;
    //总金币显示
    GameObject txtTotalMoney;
    //当前显示道具的金币
    GameObject CurPropMoney;

    //已经购买了
    public Sprite unlocked;
    //没有购买
    public Sprite dontBuy;

    //道具
    List<GameObject> propsList = new List<GameObject>();
    //减速
    GameObject retarder;
    //火
    GameObject fire;
    //闪电
    GameObject lightning;
	//炮塔
	GameObject turrets;
	//加速下落
	GameObject speedUp;
	//道具索引
	int i = 0;


    public void Init()
    {
        obj = GameObject.Find("Canvas");
        shopParent = obj.transform.Find("Shop").gameObject;

        btnClose = shopParent.transform.Find("BtnClose").gameObject;
        btnBuy = shopParent.transform.Find("BtnBuy").gameObject;
        btnRight = shopParent.transform.Find("BtnRight").gameObject;
        btnLeft = shopParent.transform.Find("BtnLeft").gameObject;
        txtTotalMoney = shopParent.transform.Find("txtTotalMoney").gameObject;
        CurPropMoney = shopParent.transform.Find("CurPropMoney").gameObject;
        //道具
        retarder = shopParent.transform.Find("Retarder").gameObject;
        propsList.Add(retarder);
        lightning = shopParent.transform.Find("Lightning").gameObject;
        propsList.Add(lightning);
        fire = shopParent.transform.Find("Fire").gameObject;
        propsList.Add(fire);
		turrets = shopParent.transform.Find("Turrets").gameObject;
		propsList.Add(turrets);
		speedUp = shopParent.transform.Find("SpeedUp").gameObject;
		propsList.Add(speedUp);

		OpenPanel();
        IsBuy();
        TxtTotalMoneyUpdata();

        btnClose.GetComponent<Button>().onClick.AddListener(() =>
        {
			PlaySound("Sounds/Panel/click_start");
			Close();
        });
        btnRight.GetComponent<Button>().onClick.AddListener(() =>
        {
			PlaySound("Sounds/Panel/click_start");
			Right();
        });
        btnLeft.GetComponent<Button>().onClick.AddListener(() =>
        {
			PlaySound("Sounds/Panel/click_start");
			Left();
        });
        btnBuy.GetComponent<Button>().onClick.AddListener(() =>
        {
			PlaySound("Sounds/Panel/click_start");
			Buy();
        });
    }
	void PlaySound(string path)
	{
		AudioManager.GetInstance.Play(path);
	}
	public void OpenPanel()
    {
        gameObject.SetActive(true);
        propsList[i].SetActive(true);
        UpdateCurPropMoney();
    }

    public void ClosePanel()
    {
        gameObject.SetActive(false);
        propsList[i].SetActive(false);
        i = 0;
    }

    public void TxtTotalMoneyUpdata() {
        txtTotalMoney.GetComponent<Text>().text = PlayerDataManager.GetIntance().GetTotalMoney().ToString();
    }
    //右移按钮
    public void ShowRightProp()
    {
        if (i < propsList.Count)
        {
            propsList[i].SetActive(false);
            i++;
            if (i == propsList.Count)
            {
                i = 0;
            }
            propsList[i].SetActive(true);
            IsBuy();
        }       
    }
    //左移按钮
    public void ShowLeftProp()
    {
        if (i < propsList.Count)
        {
            propsList[i].SetActive(false);
            i--;
            if (i == propsList.Count)
            {
                i = 0;
            }
            else if(i < 0)
            {
                i = propsList.Count-1;
            }
            propsList[i].SetActive(true);
        }
        IsBuy();
    }
    public void IsBuy()
    {
        if (PlayerPrefs.HasKey(propsList[i].name))
        {
            btnBuy.GetComponent<Image>().sprite = unlocked;
            btnBuy.GetComponent<Button>().enabled = false;
        }
        else
        {
            btnBuy.GetComponent<Image>().sprite = dontBuy;
            btnBuy.GetComponent<Button>().enabled = true;
        }
    }

    public void BuyProp()
    {
        //减掉当前道具的金币
       bool IsButFinish = PlayerDataManager.GetIntance().SubMoney(int.Parse(CurPropMoney.GetComponent<Text>().text));
       if (IsButFinish)
       {
            Debug.Log(propsList[i].name + "刚买了的道具");
            PlayerPrefs.SetString(propsList[i].name, propsList[i].name);
            btnBuy.GetComponent<Image>().sprite = unlocked;
            btnBuy.GetComponent<Button>().enabled = false;
			GameConfig.PropList.Add(propsList[i].name);
	   }
        //在显示
        TxtTotalMoneyUpdata();
    }

    public void UpdateCurPropMoney()
    {
        string text = "";
        switch (propsList[i].name)
        {
            case "Retarder":
                text = "500";
                break;
            case "Lightning":
                text = "2000";
                break;
            case "Fire":
                text = "4000";
                break;
			case "Turrets":
				text = "8000";
				break;			
			case "SpeedUp":
				text = "12000";
				break;
			
		}
        CurPropMoney.GetComponent<Text>().text = text;
    }

}
